package ecustom.common.servicelog.bean;

import java.util.Date;

import org.apache.log4j.Logger;

import ecustom.common.CustomMap;
import ecustom.common.servicelog.service.ServiceLogBodyService;
import ecustom.common.servicelog.service.ServiceLogService;
import ecustom.common.util.CustomUtil;

/**
 * 日志组件
 * @author 程序员
 */
public class ServiceLogBean {

	private String serviceCode;
	private String url;
	private String serverCode;
	private String serverIp;
	private String clientCode;
	private String clientIp;
	private Integer status;
	private long startTime;
	private long endTime;
	private double seconds;
	private Integer reqBodyId;
	private String reqBody;
	private Integer resBodyId;
	private String resBody;
	
	private Logger log = Logger.getLogger(getClass());
	private ServiceLogService logService = new ServiceLogService();
	private ServiceLogBodyService bodyService = new ServiceLogBodyService();
	
	public ServiceLogBean(String serviceCode, String serverCode, String clientCode, String url) {
		this.serviceCode = serviceCode;
		this.serverCode = serverCode;
		this.clientCode = clientCode;
		this.url = url;
	}
	
	public ServiceLogBean(String serviceCode, String serverCode, String clientCode, String url, long startTime) {
		this.serviceCode = serviceCode;
		this.serverCode = serverCode;
		this.clientCode = clientCode;
		this.url = url;
		this.startTime = startTime;
	}
	
	public void save() {
		try {
			saveReqBody();
			saveResBody();
			CustomMap map = new CustomMap();
			map.put("SERVICECODE", serviceCode);	// 服务编码
			map.put("URL", getUrlCK());	// 服务地址
			map.put("SERVERCODE", serverCode);	// 服务端系统编码
			map.put("SERVERIP", serverIp);	// 服务端系统IP
			map.put("CLIENTCODE", clientCode);	// 客户端系统编码
			map.put("CLIENTIP", clientIp);	// 客户端系统IP
			map.put("STATUS", getStatusCK());	// 结果标识
			map.put("STARTTIME", CustomUtil.getStringDate(new Date(getStartTimeCK()), "yyyy-MM-dd HH:mm:ss.SSS"));	// 调用开始时间
			map.put("ENDTIME", CustomUtil.getStringDate(new Date(getEndTimeCK()), "yyyy-MM-dd HH:mm:ss.SSS"));	// 调用结束时间
			map.put("SECONDS", getSeconds());	// 耗时（秒）
			map.put("REQBODYID", getReqBodyId());	// 请求报文编码
			map.put("RESBODYID", getResBodyId());	// 响应报文编码
			logService.save(map);
		} catch (Exception e) {
			log.error("save()", e);
		}		
	}
	
	public ServiceLogBean openLog(String serviceCode, String url) {
		ServiceLogBean logBean = new ServiceLogBean(serviceCode, "E9", "K3Cloud", url);
		logBean.setStartTime(System.currentTimeMillis());
		return logBean;
	}
	
	public void saveLog(ServiceLogBean logBean) {
		try {
			logBean.setEndTime(System.currentTimeMillis());
			logBean.setClientIp("");
			logBean.setServerIp(CustomUtil.getLocalIP());
			save();
		} catch (Exception e) {
			log.error("ServiceLogBean:"+e.getMessage());
		}
	}
	
	public void saveReqBody() throws Exception {
		if (reqBodyId != null && reqBodyId > 0) return ;
		
		if (reqBody != null && !reqBody.isEmpty())
			reqBodyId = bodyService.save(reqBody);
	}
	
	public void saveResBody() throws Exception {
		if (resBodyId != null && resBodyId > 0) return ;
		
		if (resBody != null && !resBody.isEmpty())
			resBodyId = bodyService.save(resBody);
	}
	
	public String getServiceCode() {
		return serviceCode;
	}
	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}
	public String getUrl() {
		return url;
	}
	public String getUrlCK() {
		if (url == null)
			throw new RuntimeException("URL不能为空，url: " + url);
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getServerCode() {
		return serverCode;
	}
	public void setServerCode(String serverCode) {
		this.serverCode = serverCode;
	}
	public String getServerIp() {
		return serverIp;
	}
	public void setServerIp(String serverIp) {
		this.serverIp = serverIp;
	}
	public String getClientCode() {
		return clientCode;
	}
	public void setClientCode(String clientCode) {
		this.clientCode = clientCode;
	}
	public String getClientIp() {
		return clientIp;
	}
	public void setClientIp(String clientIp) {
		this.clientIp = clientIp;
	}
	public Integer getStatus() {
		return status;
	}
	public Integer getStatusCK() {
		if (status == null)
			throw new RuntimeException("结果状态不能为空，status: " + status);
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public long getStartTime() {
		return startTime;
	}
	public long getStartTimeCK() {
		if (startTime <= 0)
			throw new RuntimeException("调用开始时间戳必须大于0，startTime: " + startTime);
		return startTime;
	}
	public void setStartTime(long startTime) {
		this.startTime = startTime;
		if (startTime > 0 && startTime < endTime)
			seconds = ((endTime - startTime) / 10) / 100.0;
	}
	public long getEndTime() {
		return endTime;
	}
	public long getEndTimeCK() {
		if (endTime <= 0)
			throw new RuntimeException("调用结束时间戳必须大于0，endTime: " + endTime);
		return endTime;
	}
	public void setEndTime(long endTime) {
		this.endTime = endTime;
		if (startTime > 0 && startTime < endTime)
			seconds = ((endTime - startTime) / 10) / 1000.0;
	}
	public double getSeconds() {
		return seconds;
	}
	public void setSeconds(double seconds) {
		this.seconds = seconds;
	}
	public Integer getReqBodyId() {
		return reqBodyId;
	}
	public void setReqBodyId(Integer reqBodyId) {
		this.reqBodyId = reqBodyId;
	}
	public String getReqBody() {
		return reqBody;
	}
	public void setReqBody(String reqBody) {
		this.reqBody = reqBody;
	}
	public String getResBody() {
		return resBody;
	}
	public void setResBody(String resBody) {
		this.resBody = resBody;
	}
	public Integer getResBodyId() {
		return resBodyId;
	}
	public void setResBodyId(Integer resBodyId) {
		this.resBodyId = resBodyId;
	}
}